# delimit ;
clear ;
set more off ;
version 12.0 ;

cd "replication" ;

* ***************************************************************************** ;
* Carpena, Fenella, and Bilal Zia. "The causal mechanism of financial education: 
* Evidence from mediation analysis." Journal of Economic Behavior & Organization 
* 177 (2020): 143-184.
* 
* This do-file creates Appendix Figures A01-A04 of the paper
* ***************************************************************************** ;

* ***************************************************************************** ;
* get endline data
* ***************************************************************************** ;

use "./data/endline.dta", clear ;

* merge with baseline ;
merge 1:1 id using "./data/baseline.dta" ;
tab _merge ;

* correction to the variable label ;
label var savings_bank "has bank savings account" ;

* ***************************************************************************** ;
* sample selection
* ***************************************************************************** ;

* drop households with baseline but no endline ;
drop if _merge == 2 ; 
drop _merge ;

* drop households with no financial knowledge scores ; 
egen temp_knowl = rowmean(numer_fin_return-attit_budget) ;
drop if temp_knowl == . ;
drop temp_knowl ; 

* identify respondents with endline measures of numeracy ;
merge 1:1 id using "./data/financial-knowledge-short-term", keepusing(id) ;
tab _merge ;
keep if _merge == 3 ;
drop _merge ;

* drop households with missing discount rate (control variable) ;
drop if disc_rate == . ;

* ***************************************************************************** ;
* create financial knowledge scores
* ***************************************************************************** ;

egen end_numeracy = rowmean(numer_fin_return-numer_int_rate) ;
egen end_awareness = rowmean(aware_budget-aware_unprod) ;
egen end_attitudes = rowmean(attit_suggest_ins-attit_budget) ;

* ***************************************************************************** ;
* create treatment dummies
* ***************************************************************************** ;

gen flcounsgoal = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 1)  ;
gen flcouns = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 0) ;
gen flgoal = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 1) ;
gen fl = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 0) ;
gen control = (fin_ed_treatment == 0 & couns_treatment == 0 & goal_treatment == 0) ;

* ***************************************************************************** ;
* control variables
* ***************************************************************************** ;

local cvars has_hard_time_saving interested_in_financial inconsistent disc_rate riskaverse  ;

* ***************************************************************************** ;
* rename variable names to make them shorter ;
* ***************************************************************************** ;

ren budget_helpful bdg_help ;
ren budget_tried bdg_tried ;
ren budget_regular bdg_reg ;
ren savings_informal sav_inf ;
ren savings_bank sav_bnk ;
ren loan_purpose_bus_educ loan_bus ; 
ren ins_life ins_life ;

* ***************************************************************************** ;
* APPENDIX FIGURE A1: FL only 
* ***************************************************************************** ;

local outcomes bdg_tried ; 

* first regression, mediator on treatment ; 
areg end_awareness fl `cvars' if fl | control, a(strata) clu(wave_class); 
predict aware_resid, residuals ;

areg end_attitudes fl `cvars' if fl | control, a(strata) clu(wave_class); 
predict attit_resid, residuals ;

* second regression, outcome on treatment + mediator ; 
foreach y of local outcomes { ; 
	areg `y' fl end_awareness `cvars' if fl | control, a(strata) clu(wave_class) ;
	predict `y'_aware_resid, resid ;

	areg `y' fl end_attitudes `cvars' if fl | control, a(strata) clu(wave_class) ;
	predict `y'_attit_resid, resid ;
} ;

* correlations ;
correlate aware_resid bdg_tried_aware_resid ;
correlate attit_resid bdg_tried_attit_resid ;

* scatter plot ;
foreach y of local outcomes { ;
	twoway (scatter aware_resid `y'_aware_resid) (lfit aware_resid `y'_aware_resid), xtitle("Sample residuals {&epsilon}{sub:3i}") ytitle("Sample residuals {&epsilon}{sub:2i}") legend(off) ; 
	graph export "./output/appendix-figure-A01-`y'-awareness.png", replace ;
	
	twoway (scatter attit_resid `y'_aware_resid) (lfit aware_resid `y'_aware_resid), xtitle("Sample residuals {&epsilon}{sub:3i}") ytitle("Sample residuals {&epsilon}{sub:2i}") legend(off) ;
	graph export "./output/appendix-figure-A01-`y'-attitudes.png", replace ;
} ;

* ***************************************************************************** ;
* APPENDIX FIGURE A2: FL + goal
* ***************************************************************************** ;

est drop _all ;
drop *resid* ;

local outcomes bdg_tried sav_bnk ; 

* first regression, mediator on treatment ; 
areg end_awareness flgoal `cvars' if flgoal | control, a(strata) clu(wave_class); 
predict aware_resid, residuals ;

areg end_attitudes flgoal `cvars' if flgoal | control, a(strata) clu(wave_class); 
predict attit_resid, residuals ;

* second regression, outcome on treatment + mediator ; 
foreach y of local outcomes { ; 
	areg `y' flgoal end_awareness `cvars' if flgoal | control, a(strata) clu(wave_class) ;
	predict `y'_aware_resid, resid ;

	areg `y' flgoal end_attitudes `cvars' if flgoal | control, a(strata) clu(wave_class) ;
	predict `y'_attit_resid, resid ;
} ;

* correlations ;
correlate aware_resid bdg_tried_aware_resid ;
correlate aware_resid sav_bnk_aware_resid ;

correlate attit_resid bdg_tried_attit_resid ;
correlate attit_resid sav_bnk_attit_resid ;

* scatter plot ;
foreach y of local outcomes { ;
	twoway (scatter aware_resid `y'_aware_resid) (lfit aware_resid `y'_aware_resid), xtitle("Sample residuals {&epsilon}{sub:3i}") ytitle("Sample residuals {&epsilon}{sub:2i}") legend(off) ; 
	graph export "./output/appendix-figure-A02-`y'-awareness.png", replace ;
	
	twoway (scatter attit_resid `y'_aware_resid) (lfit aware_resid `y'_aware_resid), xtitle("Sample residuals {&epsilon}{sub:3i}") ytitle("Sample residuals {&epsilon}{sub:2i}") legend(off) ;
	graph export "./output/appendix-figure-A02-`y'-attitudes.png", replace ;
} ;

* ***************************************************************************** ;
* APPENDIX FIGURE A3: FL + couns
* ***************************************************************************** ;

est drop _all ;
drop *resid* ;

local outcomes bdg_tried sav_bnk loan_bus ins_life ; 

* first regression, mediator on treatment ; 
areg end_awareness flcouns `cvars' if flcouns | control, a(strata) clu(wave_class); 
predict aware_resid, residuals ;

areg end_attitudes flcouns `cvars' if flcouns | control, a(strata) clu(wave_class); 
predict attit_resid, residuals ;

* second regression, outcome on treatment + mediator ; 
foreach y of local outcomes { ; 
	areg `y' flcouns end_awareness `cvars' if flcouns | control, a(strata) clu(wave_class) ;
	predict `y'_aware_resid, resid ;

	areg `y' flcouns end_attitudes `cvars' if flcouns | control, a(strata) clu(wave_class) ;
	predict `y'_attit_resid, resid ;
} ;

* correlations
correlate aware_resid bdg_tried_aware_resid ;
correlate aware_resid sav_bnk_aware_resid ;
correlate aware_resid loan_bus_aware_resid ;
correlate aware_resid ins_life_aware_resid ;

correlate attit_resid bdg_tried_attit_resid ;
correlate attit_resid sav_bnk_attit_resid ;
correlate attit_resid loan_bus_attit_resid ;
correlate attit_resid ins_life_attit_resid ;

* scatter plot ;
foreach y of local outcomes { ;
	twoway (scatter aware_resid `y'_aware_resid) (lfit aware_resid `y'_aware_resid), xtitle("Sample residuals {&epsilon}{sub:3i}") ytitle("Sample residuals {&epsilon}{sub:2i}") legend(off) ; 
	graph export "./output/appendix-figure-A03-`y'-awareness.png", replace ;
	
	twoway (scatter attit_resid `y'_aware_resid) (lfit aware_resid `y'_aware_resid), xtitle("Sample residuals {&epsilon}{sub:3i}") ytitle("Sample residuals {&epsilon}{sub:2i}") legend(off) ;
	graph export "./output/appendix-figure-A03-`y'-attitudes.png", replace ;
} ;

* ***************************************************************************** ;
* APPENDIX FIGURE A4: FL + couns + goal
* ***************************************************************************** ;

est drop _all ;
drop *resid* ;

local outcomes bdg_tried sav_bnk ins_life ; 

* first regression, mediator on treatment ; 
areg end_awareness flcounsgoal `cvars' if flcounsgoal | control, a(strata) clu(wave_class); 
predict aware_resid, residuals ;

areg end_attitudes flcounsgoal `cvars' if flcounsgoal | control, a(strata) clu(wave_class); 
predict attit_resid, residuals ;

* second regression, outcome on treatment + mediator ; 
foreach y of local outcomes { ; 
	areg `y' flcounsgoal end_awareness `cvars' if flcounsgoal | control, a(strata) clu(wave_class) ;
	predict `y'_aware_resid, resid ;

	areg `y' flcounsgoal end_attitudes `cvars' if flcounsgoal | control, a(strata) clu(wave_class) ;
	predict `y'_attit_resid, resid ;
} ;

* correlations ;
correlate aware_resid bdg_tried_aware_resid ;
correlate aware_resid sav_bnk_aware_resid ;
correlate aware_resid ins_life_aware_resid ;

correlate attit_resid bdg_tried_attit_resid ;
correlate attit_resid sav_bnk_attit_resid ;
correlate attit_resid ins_life_attit_resid ;

* scatter plot ;
foreach y of local outcomes { ;
	twoway (scatter aware_resid `y'_aware_resid) (lfit aware_resid `y'_aware_resid), xtitle("Sample residuals {&epsilon}{sub:3i}") ytitle("Sample residuals {&epsilon}{sub:2i}") legend(off) ; 
	graph export "./output/appendix-figure-A04-`y'-awareness.png", replace ;
	
	twoway (scatter attit_resid `y'_aware_resid) (lfit aware_resid `y'_aware_resid), xtitle("Sample residuals {&epsilon}{sub:3i}") ytitle("Sample residuals {&epsilon}{sub:2i}") legend(off) ;
	graph export "./output/appendix-figure-A05-`y'-attitudes.png", replace ;
} ;

exit ;
